﻿#if NETFX || NETCORE

using Apewer.Internals.Interop;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Apewer.Surface
{

    /// <summary>
    /// 
    /// </summary>
    public class BlockForm : BaseForm
    {

        #region this

        private bool _haveshadow = false;

        /// <summary></summary>
        public BlockForm()
        {
            Optimize();
        }

        /// <summary>包含窗体阴影。</summary>
        public bool HaveShadow
        {
            get { return _haveshadow; }
            set
            {
                _haveshadow = value;
                if (value) { if (_shadow != null) HideShadow(); }
                else { if (_shadow == null) ShowShadow(); }
            }
        }

        #endregion

        #region override

        /// <summary>显示或隐藏时的动画和阴影。</summary>
        protected override void OnVisibleChanged(EventArgs e)
        {
            if (Visible)
            {
                if (!DesignMode) User32.AnimateWindow(this.Handle, 150, Constant.AW_BLEND | Constant.AW_ACTIVATE);
                if (!DesignMode && _shadow == null) ShowShadow();
                base.OnVisibleChanged(e);
            }
            else
            {
                base.OnVisibleChanged(e);
                User32.AnimateWindow(this.Handle, 150, Constant.AW_BLEND | Constant.AW_HIDE);
            }
        }

        /// <summary>关闭窗体时同时关闭阴影。</summary>
        protected override void OnClosing(CancelEventArgs e)
        {
            HideShadow();
            base.OnClosing(e);
            User32.AnimateWindow(this.Handle, 150, Constant.AW_BLEND | Constant.AW_HIDE);
        }

        #endregion

        #region surface

        /// <summary>
        /// 
        /// </summary>
        /// <param name="color"></param>
        /// <param name="titleHeight"></param>
        /// <param name="border"></param>
        public void PaintWall(Color color, int titleHeight, bool border = true)
        {
            var bitmap = new Bitmap(Width, Height);
            var graphic = Graphics.FromImage(bitmap);
            var brush = new SolidBrush(color);
            var pen = new Pen(color);

            graphic.FillRectangle(brush, 0, 0, Width, titleHeight);
            if (border) graphic.DrawRectangle(pen, 0, 0, Width - 1, Height - 1);

            pen.Dispose();
            brush.Dispose();
            graphic.Dispose();

            if (BackgroundImage != null) BackgroundImage.Dispose();
            BackgroundImage = bitmap;
        }

        #endregion

        #region shadow

        /// <summary>窗体阴影边框。</summary>
        private BlockShadow _shadow;

        private void ShowShadow()
        {
            if (!HaveShadow) return;
            _shadow = new BlockShadow(this);
            _shadow.Show(this);
        }


        private void HideShadow()
        {
            if (_shadow != null)
            {
                _shadow.Close();
                _shadow.Dispose();
                _shadow = null;
            }
        }

        /// <summary>阴影已溢出屏幕。</summary>
        public bool ShadowOverflow
        {
            get
            {
                int vwidth = Width + 200;
                int vheight = Height + 200;
                if ((vwidth > Screen.PrimaryScreen.Bounds.Width) || (vheight > Screen.PrimaryScreen.Bounds.Height))
                {
                    return true;
                }
                else return false;
            }
        }

        #endregion

    }
}

#endif
